﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class WeiXinRule
    {
        public int Add(Chain.Model.WeiXinRule model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into WeiXinRule(");
            builder.Append("RuleNUmber,RuleNewsType,RuleDesc,RuleContent,RuleUserID,RuleCreateTime)");
            builder.Append(" values (");
            builder.Append("@RuleNUmber,@RuleNewsType,@RuleDesc,@RuleContent,@RuleUserID,@RuleCreateTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleNUmber", SqlDbType.NVarChar, 50), new SqlParameter("@RuleNewsType", SqlDbType.NVarChar, 50), new SqlParameter("@RuleDesc", SqlDbType.NVarChar, 500), new SqlParameter("@RuleContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@RuleUserID", SqlDbType.Int, 4), new SqlParameter("@RuleCreateTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.RuleNUmber;
            cmdParms[1].Value = model.RuleNewsType;
            cmdParms[2].Value = model.RuleDesc;
            cmdParms[3].Value = model.RuleContent;
            cmdParms[4].Value = model.RuleUserID;
            cmdParms[5].Value = model.RuleCreateTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataTable Attention()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from (select *, ROW_NUMBER () over (order by RuleID)as a from WeiXinRule where RuleID<=4) as a");
            builder.Append(" union all ");
            builder.Append("select * from (select *, ROW_NUMBER () over (order by RuleID)as a from WeiXinRule where RuleID>=5) as b");
            return DbHelperSQL.Query(builder.ToString()).Tables[0];
        }

        public string AttentionStr()
        {
            StringBuilder builder = new StringBuilder();
            DataTable table = this.Attention();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                builder.AppendLine(row["RuleDesc"].ToString()).AppendLine();
            }
            return builder.ToString();
        }

        public bool Delete(int RuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinRule ");
            builder.Append(" where RuleID=@RuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RuleID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string RuleIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinRule ");
            builder.Append(" where RuleID in (" + RuleIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public string ErrorStr()
        {
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            builder2.Append("select * from (select *, ROW_NUMBER () over (order by RuleID)as a from WeiXinRule where RuleID<=4) as a");
            builder2.Append(" union all ");
            builder2.Append("select * from (select *, ROW_NUMBER () over (order by RuleID desc)as a from WeiXinRule where RuleID>=5) as b");
            DataTable table = DbHelperSQL.Query(builder2.ToString()).Tables[0];
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                builder.AppendLine(row["RuleDesc"].ToString()).AppendLine();
            }
            return builder.ToString();
        }

        public bool Exists(int RuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from WeiXinRule");
            builder.Append(" where RuleID=@RuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RuleID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public string GetCardDesc()
        {
            string str = "";
            DataTable table = DbHelperSQL.Query("select RuleContent from WeiXinRule where RuleNUmber='-1'").Tables[0];
            if (table.Rows.Count > 0)
            {
                str = table.Rows[0][0].ToString();
            }
            return str;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RuleID,RuleNUmber,RuleNewsType,RuleDesc,RuleContent,RuleUserID,RuleCreateTime,UserName ");
            builder.Append("FROM WeiXinRule,SysUser ");
            builder.Append("where UserID=RuleUserID ");
            if (strWhere.Trim() != "")
            {
                builder.Append("and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" RuleID,RuleNUmber,RuleNewsType,RuleDesc,RuleContent,RuleUserID,RuleCreateTime ");
            builder.Append(" FROM WeiXinRule ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.RuleID desc");
            }
            builder.Append(")AS Row, T.*  from WeiXinRule T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("RuleID", "WeiXinRule");
        }

        public Chain.Model.WeiXinRule GetModel(int RuleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 RuleID,RuleNUmber,RuleNewsType,RuleDesc,RuleContent,RuleUserID,RuleCreateTime from WeiXinRule ");
            builder.Append(" where RuleID=@RuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RuleID;
            Chain.Model.WeiXinRule rule = new Chain.Model.WeiXinRule();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["RuleID"] != null) && (set.Tables[0].Rows[0]["RuleID"].ToString() != ""))
            {
                rule.RuleID = int.Parse(set.Tables[0].Rows[0]["RuleID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RuleNUmber"] != null) && (set.Tables[0].Rows[0]["RuleNUmber"].ToString() != ""))
            {
                rule.RuleNUmber = set.Tables[0].Rows[0]["RuleNUmber"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RuleNewsType"] != null) && (set.Tables[0].Rows[0]["RuleNewsType"].ToString() != ""))
            {
                rule.RuleNewsType = set.Tables[0].Rows[0]["RuleNewsType"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RuleDesc"] != null) && (set.Tables[0].Rows[0]["RuleDesc"].ToString() != ""))
            {
                rule.RuleDesc = set.Tables[0].Rows[0]["RuleDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RuleContent"] != null) && (set.Tables[0].Rows[0]["RuleContent"].ToString() != ""))
            {
                rule.RuleContent = set.Tables[0].Rows[0]["RuleContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["RuleUserID"] != null) && (set.Tables[0].Rows[0]["RuleUserID"].ToString() != ""))
            {
                rule.RuleUserID = int.Parse(set.Tables[0].Rows[0]["RuleUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["RuleCreateTime"] != null) && (set.Tables[0].Rows[0]["RuleCreateTime"].ToString() != ""))
            {
                rule.RuleCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["RuleCreateTime"].ToString());
            }
            return rule;
        }

        public Chain.Model.WeiXinRule GetModelByNewsRuleID(string RuleNUmber)
        {
            DataTable table = DbHelperSQL.Query("select * from WeiXinRule where RuleNUmber=@RuleNUmber", new SqlParameter[] { new SqlParameter("@RuleNUmber", RuleNUmber) }).Tables[0];
            if (table.Rows.Count > 0)
            {
                return this.GetModel(int.Parse(table.Rows[0]["RuleID"].ToString()));
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM WeiXinRule ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public string Reply1()
        {
            return DbHelperSQL.Query("select RuleContent from WeiXinRule where RuleNUmber='1'").Tables[0].Rows[0][0].ToString();
        }

        public string Reply2()
        {
            return DbHelperSQL.Query("select RuleContent from WeiXinRule where RuleNUmber='2'").Tables[0].Rows[0][0].ToString();
        }

        public bool Update(Chain.Model.WeiXinRule model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update WeiXinRule set ");
            builder.Append("RuleNUmber=@RuleNUmber,");
            builder.Append("RuleNewsType=@RuleNewsType,");
            builder.Append("RuleDesc=@RuleDesc,");
            builder.Append("RuleContent=@RuleContent,");
            builder.Append("RuleUserID=@RuleUserID,");
            builder.Append("RuleCreateTime=@RuleCreateTime");
            builder.Append(" where RuleID=@RuleID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleNUmber", SqlDbType.NVarChar, 50), new SqlParameter("@RuleNewsType", SqlDbType.NVarChar, 50), new SqlParameter("@RuleDesc", SqlDbType.NVarChar, 500), new SqlParameter("@RuleContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@RuleUserID", SqlDbType.Int, 4), new SqlParameter("@RuleCreateTime", SqlDbType.DateTime), new SqlParameter("@RuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.RuleNUmber;
            cmdParms[1].Value = model.RuleNewsType;
            cmdParms[2].Value = model.RuleDesc;
            cmdParms[3].Value = model.RuleContent;
            cmdParms[4].Value = model.RuleUserID;
            cmdParms[5].Value = model.RuleCreateTime;
            cmdParms[6].Value = model.RuleID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

